package sz.com.postoffice.jxyzwgcase.service.impl;

import com.github.pagehelper.Page;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import sz.com.postoffice.jxyzwgcase.core.TableDataInfo;
import sz.com.postoffice.jxyzwgcase.dao.BaseDao;
import sz.com.postoffice.jxyzwgcase.dao.master.GridModelDao;
import sz.com.postoffice.jxyzwgcase.dao.master.ResourceMarketDao;
import sz.com.postoffice.jxyzwgcase.pojo.GridModel;
import sz.com.postoffice.jxyzwgcase.pojo.ResourceMarket;
import sz.com.postoffice.jxyzwgcase.service.ResourceMarketService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName ResourcServcieImpl
 * @Deacription 资源逻辑处理层
 * @Author jmx
 * @Date 2020/11/20 18:00
 * @Version 1.0
 **/
@Service
@Slf4j
public class ResourceMarketServiceImpl extends BaseServiceImpl<ResourceMarket> implements ResourceMarketService {
    private static final Logger logger= LoggerFactory.getLogger(ResourceMarketServiceImpl.class);
    @Resource
    private ResourceMarketDao resourceDao;
    @Override
    protected BaseDao<ResourceMarket> getMapper() {
        return this.resourceDao;
    }
    @Resource
    private GridModelDao gridModelDao;

    @Override
    public boolean updateByType(ResourceMarket market) {
        boolean flag = true;
        try {
            switch (market.getResourceType()){
                case 1:
                    resourceDao.updateCampus(market);
                    break;
                case 2:
                    resourceDao.updateEnterprise(market);
                    break;
                case 3:
                    resourceDao.updateBuilding(market);
                    break;
                case 4:
                    resourceDao.updateUptown(market);
                    break;
                default:
                    System.out.println("type is not defined...");
                    return false;
            }
        } catch (Exception e) {
            flag = false;
            logger.error("更新资源失败!原因是:",e);
        }
        return flag;
    }

    @Override
    public boolean deleteByTypeAndId(ResourceMarket market) {
        boolean flag = true;
        try {
            switch (market.getResourceType()){
                case 1:
                    resourceDao.deleteCampusById(market.getId());
                    break;
                case 2:
                    resourceDao.deleteEnterpriseById(market.getId());
                    break;
                case 3:
                    resourceDao.deleteBuildingById(market.getId());
                    break;
                case 4:
                    resourceDao.deleteUptownById(market.getId());
                    break;
                default:
                    System.out.println("type is not defined...");
                    return false;
            }
        } catch (Exception e) {
            flag = false;
            logger.error("删除资源失败!原因是:",e);
        }
        return flag;
    }

    @Override
    public boolean deleteByTypeAndCode(ResourceMarket market) {
        boolean flag = true;
        try {
            switch (market.getResourceType()){
                case 1:
                    resourceDao.deleteCampusByCode(market.getCode());
                    break;
                case 2:
                    resourceDao.deleteEnterpriseByCode(market.getCode());
                    break;
                case 3:
                    resourceDao.deleteBuildingByCode(market.getCode());
                    break;
                case 4:
                    resourceDao.deleteUptownByCode(market.getCode());
                    break;
                default:
                    System.out.println("type is not defined...");
                    return false;
            }
        } catch (Exception e) {
            flag = false;
            logger.error("删除资源失败!原因是:",e);
        }
        return flag;
    }

    @Override
    public boolean insertByType(ResourceMarket market) {
        boolean flag = true;
        try {
            switch (market.getResourceType()){
                case 1:
                    market.setType("0");
                    resourceDao.insertCampus(market);
                    break;
                case 2:
                    market.setType("wkt");
                    resourceDao.insertEnterprise(market);
                    break;
                case 3:
                    resourceDao.insertBuilding(market);
                    break;
                case 4:
                    market.setType("0");
                    resourceDao.insertUptown(market);
                    break;
                default:
                    System.out.println("type is not defined...");
            }
        } catch (Exception e) {
            flag = false;
            logger.error("新增资源失败!原因是:",e);

        }
        return flag;
    }

    @Override
    public ResourceMarket findByTypeAndId(ResourceMarket market) {
        ResourceMarket resourceMarket = null;
        switch (market.getResourceType()){
            case 1:
                resourceMarket = resourceDao.findCampusById(market.getId());
                break;
            case 2:
                resourceMarket = resourceDao.findEnterpriseById(market.getId());
                break;
            case 3:
                resourceMarket = resourceDao.findBuildingById(market.getId());
                break;
            case 4:
                resourceMarket = resourceDao.findUptownById(market.getId());
                break;
            default:
                System.out.println("type is not defined...");
        }
        if (resourceMarket != null) {
            if (StringUtils.isNotEmpty(resourceMarket.getGridCode())) {
                GridModel gridModel = gridModelDao.findByCode(resourceMarket.getGridCode());
                if (gridModel != null) {
                    resourceMarket.setGridCodePath(gridModel.getAllParentCode());
                }
            }
            String wkt = resourceMarket.getWkt();
            if (StringUtils.isNotEmpty(wkt)) {
                String str = wkt.substring(wkt.lastIndexOf("(") + 1, wkt.indexOf(")"));
                String[] wktList = str.split(",");
                resourceMarket.setWktList(wktList);
            }

        }
        return resourceMarket;
    }

    @Override
    public TableDataInfo findPageListByType(ResourceMarket market,int pageSize, int pageNum) {
        if (StringUtils.isNotEmpty(market.getDeptCode())) {
            List<ResourceMarket> resourceMarketList = findListByTypeAndDept(market);
            List<ResourceMarket> subList = new ArrayList<>();
            for (int i = pageSize * (pageNum -1); i < pageSize * pageNum; i++){
                if (i >= resourceMarketList.size()) {
                    break;
                }
                subList.add(resourceMarketList.get(i));
            }
            TableDataInfo dataTable = new TableDataInfo();
            dataTable.setTotal(resourceMarketList.size());
            dataTable.setRows(subList);
            dataTable.setCode(200);
            dataTable.setMsg("请求成功");
            return dataTable;
        } else {
            return findPageListByTypeAndGrid(market);
        }
    }

    @Override
    public List<ResourceMarket> findListByType(ResourceMarket market) {
        if (StringUtils.isNotEmpty(market.getDeptCode())) {
            return findListByTypeAndDept(market);
        } else {
            return findByTypeAndGrid(market);
        }
    }

    @Override
    public ResourceMarket findByTypeAndCode(ResourceMarket market) {
        if (StringUtils.isEmpty(market.getCode())) {
            logger.info("param entity has no code");
            return null;
        }
        List<ResourceMarket> list = findByTypeAndGrid(market);
        if (!Collections.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    private List<ResourceMarket> findByTypeAndGrid(ResourceMarket market) {
        switch (market.getResourceType()){
            case 1:
                return resourceDao.findCampusByListEntity(market);
            case 2:
                return resourceDao.findEnterpriseByListEntity(market);
            case 3:
                return resourceDao.findBuildingByListEntity(market);
            case 4:
                return resourceDao.findUptownByListEntity(market);
            default:
                System.out.println("type is not defined...");
        }
        return null;
    }

    private TableDataInfo findPageListByTypeAndGrid(ResourceMarket market) {
        Page<?> page = startPage();
        List<ResourceMarket> resourceList = findByTypeAndGrid(market);
        return getDataTable(resourceList);
    }

    private List<ResourceMarket> findListByTypeAndDept(ResourceMarket market) {
        List<ResourceMarket> resourceList = new ArrayList<>();
        GridModel param = new GridModel();
        param.setParentCode(market.getDeptCode());
        List<GridModel> gridModelList = gridModelDao.findByListEntity(param);
        if (gridModelList != null) {
            for (GridModel gridModel : gridModelList) {
                ResourceMarket paramResource = new ResourceMarket();
                paramResource.setResourceType(market.getResourceType());
                paramResource.setGridCode(gridModel.getCode());
                paramResource.setName(market.getName());
                if (!Collections.isEmpty(findByTypeAndGrid(paramResource))) {
                    resourceList.addAll(findByTypeAndGrid(paramResource));
                }
            }
        }
        return resourceList;
    }
}
